#!/usr/bin/env bash
#! Encoding UTF-8
###################################################################
# NOTE:
# This Scripts template all rights reserved deserved by MickeyZZC
# Copyright  2020
###################################################################
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/mnt/data/bin
export LC_ALL=C
export PATH
clear
SELF_PATH=$(cd $(dirname "$0") && pwd)

info(){
  echo "$(date +'%F %T') [INFO] " "$@"
}
waring(){
  echo "$(date +'%F %T') [WARING] " "$@"
}
fatal(){
  echo "$(date +'%F %T') [ERROR] " "$@"
  exit 1
}

PRO_NUM=3

SCRIPTS_PATH="${SELF_PATH}/oss_curl"
LOG_FILE="${SELF_PATH}/log/download_tool.log"
mkdir -p "${SELF_PATH}/log"

#===============
# 子脚本的通用变量
#===============
# 下载根目录
if [[ -z ${DOWNLOAD_BASE_PATH} ]] ;then
  export DOWNLOAD_BASE_PATH="${HOME}/download"
fi
mkdir -p ${DOWNLOAD_BASE_PATH}/tmp
# 下载代理
#export PROXY_URL=""
#===============

CUT_LOG=" tee -a "
# https://gitee.com/mickeybee/cutlog
if [[ -x ${HOME}/bin/cutlog ]];then
  CUT_LOG=" ${HOME}/bin/cutlog -f "
fi

TMP_FIFO_FILE="/tmp/$$.fifo" \
  && mkfifo "${TMP_FIFO_FILE}" \
  && exec 6<>"${TMP_FIFO_FILE}" \
  && rm "${TMP_FIFO_FILE}"

for (( i=1; i<=${PRO_NUM}; i++ ));do
  echo
done >&6

for VAR in $(echo ${SCRIPTS_PATH}/*.sh) ;do
      read -u6
      {
      if [[ -x ${VAR} ]] ;then
        info "正在执行脚本（${VAR}）" | tee -a ${LOG_FILE}
        ${VAR} | ${CUT_LOG} ${LOG_FILE}
      fi 
      echo >&6
      } &
done
wait
exec 6>&-

